﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ImbedLibrary
{
    public class SelDAL : DAL
    {
        public SelDAL(String name, String basePath) : base(name, basePath) { }

        protected override void Render(IList<ArrayList> list, IDataReader reader)
        {
            ArrayList rowList;
            bool hasData = reader.Read();
            while (hasData)
            {
                rowList = new ArrayList();
                rowList.Add(reader[0].ToString());
                list.Add(rowList);
                hasData = reader.Read();
            }
        }

        protected String GetSqlSelect(String columnName,String field, object data)
        {
            String sql = "Select ";
            sql += GetColumnName(columnName) + " from [" + tableName + "]";
            if(field != null)
            {
                sql += " where ["  + field + "] " + data.GetData();
            } 
            return sql;
        }

        protected String GetColumnName(String columnName)
        {
            if (columnName == null) { return " * "; }
            else { return columnName; }
        }

        /// <summary>
        /// 查询表中的一个字段
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="data">条件</param>
        /// <returns>字段内容</returns>
        public String SelString(String columnName)
        {
            String sql = GetSqlSelect(columnName, null, null);
            IList<ArrayList> list = helper.ExecuteReader<ArrayList>(sql, Render);
            if (list.Count != 0)
                return list[0][0].ToString();
            return null;
        }

        /// <summary>
        /// 查询数据
        /// 可定制具体需要查询的字段
        /// </summary>
        /// <param name="column">定制字段</param>
        /// <param name="field">条件字段</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public IList<ArrayList> SelString(string column,string field,object data)
        {
            string sql = GetSqlSelect(column, field, data);
            return Sel(sql);
        }
    }
}
